<?php
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link
 * @package       Model
 * @since         MaginPHP 1.0
 * @license
 * @author        aNKIT kHAMBHATA
 * @example
 * $field = array (array("id"=>"user_name" ,
 *                       "type"=>"text"
 *                       "error_message"=> "Please Enter User Name",
 *                       "length_type"=>"eq" ,
 *                       "length_value"=>10 ,
 *                      ));
 */
?>

<?php
    class Magic_Jqueryvalidation_Model {
        /**
         * @desc: this function is add jquery message file
         * @param  $file : __file__ variable
         */
        public function  __construct($file) {
                            include Magic_FindRoot_Model::getRelevantPath($file)."log/jquery_message.php";
                            
                        }
        public function createValidation($field,$functionname,$parameters,$extra_before_request,$extra_after_request,$request_file,$action){
                            
                            $str=$functionname.'=function('.$parameters.'){';
                            $str .=$extra_before_request;
                            for($i=0;$i<count($field);$i++){
                                if(!empty($field[$i]["id"])) {
                                    $prefix='';
                                    if($field[$i]["type"] == 'textarea') {
                                        $prefix ='textarea';
                                    }

                                    $str.='if($("'.$prefix.'#'.$field[$i]["id"].'").attr("value")==""){
                                                alert("Require :'.ucwords($field[$i]["id"]).'");
                                                return false;
                                           }';

                                }
                                if (!empty($field[$i]["type"])) {
                                    switch ($field[$i]["type"]) {
                                        case 'email' : $result='if(!valid_'.$field[$i]["type"].'($("#'.$field[$i]["id"].'").attr("value"))){
                                                                      alert("Email Id is not valid !");
                                                                    return false;
                                        			}';
                                            break;

                                        case 'checkbox' : $result='if(!valid_'.$field[$i]["type"].'("'.$field[$i]["id"].'")){
                                                                      alert("Please Select Atleast One Checkbox in '.ucwords($field[$i]["id"]).'");
                                                                    return false;
                                        			}';
                                                break;
                                        case 'radio' : $result = 'if($("input:radio[name='.$field[$i]["id"].']").attr("checked") ==false){
                                                                      alert("Please Select Atleast One Checkbox in '.ucwords($field[$i]["id"]).'");
                                                                    return false;
								}';
                                                break;
                                         

                                    } // End of switch

                                    $str .= $result;
                                    $result ='';
                                }
                                if (!empty($field[$i]["length_type"])) {
                                    $str.='if(!check_length($("#'.$field[$i]["id"].'").attr("value"),"'.$field[$i]["length_type"].'","'.$field[$i]["length_value"].'")){
                                            alert("Only  '.$field[$i]["length_value"].' digit/word  requier in '.$field[$i]["id"].'");
                                            return false;
                                    }';
                                }
                                
                            }
                            $str.=$extra_after_request;
                            
                            $str .= '}';
                            return $str;
                        }
        
    } // End of class

?>
